﻿@{
    Layout = "~/Views/Shared/_Index.cshtml";
 }
<div class="container-div">
    <div class="row">
        <div id="searchDiv" class="col-sm-12 search-collapse">
            <div class="select-list">
                <ul>
                    <li>
                        群名称：<input id="name" col="name" type="text" />
                    </li>
                    <li>
                        用户状态：<span id="type" col="type"></span>
                    </li>
                    <li>
                        <a id="btnSearch" class="btn btn-primary btn-sm" onclick="searchGrid()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                    </li>
                </ul>
            </div>
        </div>
        <div class="btn-group-sm hidden-xs" id="toolbar">
            @*<a id="btnAdd" class="btn btn-success" onclick="showSaveForm(true)"><i class="fa fa-plus"></i> 新增</a>
        <a id="btnEdit" class="btn btn-primary disabled" onclick="showSaveForm(false)"><i class="fa fa-edit"></i> 修改</a>
        <a id="btnDelete" class="btn btn-danger disabled" onclick="deleteForm()"><i class="fa fa-remove"></i> 删除</a>*@
            <a id="btnDelete" class="btn btn-danger disabled" onclick="deleteForm()"><i class="fa fa-remove"></i> 删除</a>
            <a id="btnPull" class="btn btn-info" onclick="pullGroupChatForm()"><i class="fa fa-clock-o"></i> 同步</a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="gridTable" data-mobile-responsive="true"></table>
        </div>
    </div>
</div>

<script type="text/javascript">
    $(function () {
        $("#type").ysComboBox({ data: ys.getJson(@Html.Raw(typeof(chatTypeEnum).EnumToDictionaryString())) });
        initGrid();
    });

    function initGrid() {
        var queryUrl = '@Url.Content("~/QixingManage/WXCorpGroupChat/GetPageListJson")';
        $('#gridTable').ysTable({
            url: queryUrl,
            columns: [
                { checkbox: true, visible: true },
                { field: 'Id', title: 'Id', visible: false },
                { field: 'name', title: '群名称' },
                { field: 'scene_doc', title: '场景' },
                { field: 'auto_create_room_doc', title: '是否自动新建群' },
                {
                    field: 'type', title: '群类型', formatter: function (value, row, index) {
                        if (row.type == "@chatTypeEnum.永久群.ParseToInt()") {
                            return '<span class="badge badge-primary">' + "@chatTypeEnum.永久群.GetDescription()" + '</span>';
                        } else if(row.type == "@chatTypeEnum.临时群.ParseToInt()") {
                            return '<span class="badge badge-warning">' + "@chatTypeEnum.临时群.GetDescription()" + '</span>';
                        }
                        else {
                            return '<span class="badge badge-warning">' + "@chatTypeEnum.其他.GetDescription()" + '</span>';
                        }
                    }
                },
                {
                    field: 'status_doc', title: '状态', formatter: function (value, row, index) {
                        if (row.status != null)
                        {
                            return '<span class="badge badge-primary">' + row.status_doc+ '</span>';
                        }
                        else
                        {
                            return '<span class="badge badge-warning">' +row.status_doc+ '</span>';
                        }
                    }
                },
                {
                    field: 'qr_code', title: '群二维码', formatter: function (value, row, index) {
                        if (row.scene ==2) {
                            return '<img class="img-circle img-xs" src="' + row.qr_code + '" onclick=showImage("' + row.qr_code + '") />';
                        }
                        else {
                            return ' ';
                        }
                    }
                },
                { field: 'create_time', title: '创建时间' },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs" href="#" onclick="groupChatJoinWaySet(\'' + row.chat_id + '\')"><i class="fa fa-edit"></i>入群配置</a>  ');

                        if (row.config_id)
                        {
                            actions.push('<a class="btn btn-danger btn-xs" href="#" onclick="delGroupChatJoinWay(\'' + row.config_id + '\')"><i class="fa fa-remove"></i>删除配置</a>');
                        }
                        return actions.join('');
                    }
                }
            ],
            queryParams: function (params) {
                var pagination = $('#gridTable').ysTable('getPagination', params);
                var queryString = $('#searchDiv').getWebControls(pagination);
                return queryString;
            }
        });
    }

    function searchGrid() {
        $('#gridTable').ysTable('search');
        resetToolbarStatus();
    }
    function showImage(imageUrl) {
        var html = '<img src="' + imageUrl + '" width="100%" height="100%" class="img-responsive" />';
        ys.openDialogContent({
            content: html,
            width: 'auto',
            height: 'auto',
            closeBtn: true
        });
    }
    function groupChatJoinWaySet(chatid)
    {
         ys.openDialog({
             title:'入群配置',
             content: '@Url.Content("~/QixingManage/GroupChatJoinWay/GroupChatJoinWayForm")' + '?chatid=' + chatid,
            width: '768px',
            height: '550px',
            callback: function (index, layero) {
                var iframeWin = window[layero.find('iframe')[0]['name']];
                iframeWin.saveForm(index);
            }
        });
    }

    function showSaveForm(bAdd) {
        var id = 0;
        if (!bAdd) {
            var selectedRow = $('#gridTable').bootstrapTable('getSelections');
            if (!ys.checkRowEdit(selectedRow)) {
                return;
            }
            else {
                id = selectedRow[0].Id;
            }
        }
        ys.openDialog({
            title: id > 0 ? '编辑' : '添加',
            content: '@Url.Content("~/QixingManage/WXCorpGroupChat/WXCorpGroupChatForm")' + '?id=' + id,
            width: '768px',
            height: '550px',
            callback: function (index, layero) {
                var iframeWin = window[layero.find('iframe')[0]['name']];
                iframeWin.saveForm(index);
            }
        });
    }

    function pullGroupChatForm()
    {
        ys.confirm('是否更新群信息？', function () {

                ys.ajax({
                    url: '@Url.Content("~/QixingManage/GroupChatJoinWay/PullGroupChatForm")',
                    type: 'post',
                    success: function (obj) {
                        if (obj.Tag == 1) {
                            ys.msgSuccess(obj.Message);
                            searchGrid();
                        }
                        else {
                            ys.msgError(obj.Message);
                        }
                    }
                });
            });
    }

    function delGroupChatJoinWay(configid) {
        var selectedRow = $('#gridTable').bootstrapTable('getSelections');
        if (ys.checkRowDelete(selectedRow)) {
            ys.confirm('确认要删除选中的' + selectedRow.length + '条数据吗？', function () {
                var ids = ys.getIds(selectedRow);
                ys.ajax({
                    url: '@Url.Content("~/QixingManage/GroupChatJoinWay/DeleteConfigIdFormJson")' + '?configid=' + configid,
                    type: 'post',
                    success: function (obj) {
                        if (obj.Tag == 1) {
                            ys.msgSuccess(obj.Message);
                            searchGrid();
                        }
                        else {
                            ys.msgError(obj.Message);
                        }
                    }
                });
            });
        }
    }

    function deleteForm() {
        var selectedRow = $('#gridTable').bootstrapTable('getSelections');
        if (ys.checkRowDelete(selectedRow)) {
            ys.confirm('确认要删除选中的' + selectedRow.length + '条数据吗？', function () {
                var ids = ys.getIds(selectedRow);
                ys.ajax({
                    url: '@Url.Content("~/QixingManage/WXCorpGroupChat/DeleteFormJson")' + '?ids=' + ids,
                    type: 'post',
                    success: function (obj) {
                        if (obj.Tag == 1) {
                            ys.msgSuccess(obj.Message);
                            searchGrid();
                        }
                        else {
                            ys.msgError(obj.Message);
                        }
                    }
                });
            });
        }
    }
</script>
